package com.sjx.lib_base.com.sjx.lib_base.view

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.util.Log
import android.util.TypedValue
import android.view.View
import com.sjx.lib_base.R

/**
 * FileName: TemperatureLine
 * Founder: sjx
 * Profile: 逐小时天气折线图
 */
class TemperatureLine @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {

    private var mLineWidth: Int = 1 //线的宽度
    private var mDotRadius: Int = 4 //小圆点半径

    private val mDotPaint: Paint = Paint(Paint.ANTI_ALIAS_FLAG) //小圆点画笔
    private val mLinePaint: Paint = Paint(Paint.ANTI_ALIAS_FLAG) //温度折线画笔

    private var pointX = 0f
    private var viewHeight = 60f

    private var maxValue = 0
    private var minValue = 0
    private var currentValue = 0
    private var beforeValue: Int? = null
    private var nextValue: Int? = null

    private var smallestUnit = 0f
    private var pointY = 0f

    init {
        init(attrs, defStyleAttr)
        initPaint()
    }

    @SuppressLint("CustomViewStyleable")
    private fun init(attrs: AttributeSet?, defStyleAttr: Int) {
        val a = context.obtainStyledAttributes(attrs, R.styleable.WeatherLineView, defStyleAttr, 0)
        with(a) {
            mLineWidth =
                getDimension(R.styleable.WeatherLineView_lineWidth, dp2px(mLineWidth)).toInt()
            mDotRadius =
                getDimension(R.styleable.WeatherLineView_dotRadius, dp2px(mDotRadius)).toInt()
            recycle()
        }
    }

    private fun initPaint() {
        with(mDotPaint) {
            style = Paint.Style.FILL
            color = resources.getColor(R.color.red)
            isAntiAlias = true
        }
        with(mLinePaint) {
            style = Paint.Style.STROKE
            strokeWidth = mLineWidth.toFloat()
            color = resources.getColor(R.color.red)
            strokeJoin = Paint.Join.ROUND
            isAntiAlias = true
        }
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        canvas.save()  // 保存当前坐标系状态
        canvas.translate(0f, 30f)
        drawDot(canvas)
        drawLine(canvas)
    }


    @Override
    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        setMeasuredDimension(
            widthMeasureSpec,
            180
        )
        val viewWidth = measuredWidth
        pointX = viewWidth / 2f
        Log.d("TAG", "onMeasure: width:$viewWidth height$viewHeight")
    }

    private fun drawDot(canvas: Canvas) {
        canvas.drawCircle(pointX, pointY, mDotRadius.toFloat(), mDotPaint)
    }

    private fun drawLine(canvas: Canvas) {
        //(画右线)
        if (nextValue != null) {
            val middleValue = currentValue - (currentValue - nextValue!!) / 2f
            val middleY = viewHeight - smallestUnit * (middleValue - minValue)
            canvas.drawLine(pointX, pointY, pointX * 2, middleY, mLinePaint)
        }

        //(画左线)
        if (beforeValue != null) {
            val middleValue = currentValue - (currentValue - beforeValue!!) / 2f
            val middleY = viewHeight - smallestUnit * (middleValue - minValue)
            canvas.drawLine(0f, middleY, pointX, pointY, mLinePaint)
        }
    }

    fun setTemperatureValue(
        maxValue: Int,
        minValue: Int,
        currentValue: Int,
        beforeValue: Int?,
        lastValue: Int?
    ) {
        this.maxValue = maxValue
        this.minValue = minValue
        this.currentValue = currentValue
        this.beforeValue = beforeValue
        this.nextValue = lastValue
        this.smallestUnit = viewHeight / (maxValue - minValue).toFloat()
        this.pointY = viewHeight - smallestUnit * (currentValue - minValue)
    }

    private fun dp2px(dpVal: Int): Float {
        return TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP,
            dpVal.toFloat(),
            resources.displayMetrics
        )
    }
}
